<?php

/**
 * @copyright Nichlas Severinsen
 * @license   https://opensource.org/licenses/BSD-2-Clause
 */

namespace Xxh3;

use Necklace\XxHash\Xxh3\XxHash128;
use PHPUnit\Framework\TestCase;
use Exception;

final class XxHash128Test extends TestCase
{
	private $x;

	protected function setUp(): void
	{
		try {
			$this->x = new XxHash128;
		} catch (Exception $e) {
			if($e->getMessage() === 'Too old version of libxxhash. XXH3 requires v0.7.1 or above.')
			{
				$this->markTestSkipped('Not applicable for version.');
			}
		}
	}

	protected function teardown(): void
	{
		unset($this->x);
	}

	public function testHash(): void
	{
		$this->assertEquals('2e790d069a07b05183ff80eedd51d542', $this->x->hash('test'));
	}

	public function testHashWithSeed(): void
	{
		$this->assertEquals('2409be6c8b4ea1577c601857728eb3f6', $this->x->hash('test', 1));

		$this->assertEquals('7a4aef0e7f4f099ff4673c78b49c1c7a', $this->x->hash('test', 10));

		$this->assertEquals('0668c111a593fea43a81538f866b929a', $this->x->hash('test', 100));
	}

	public function testHashWithBadSecret(): void
	{
		$this->expectExceptionMessage('XxHash XXH3 secret is too small.');

		$this->x->hash('test', null, 'not long enough secret');
	}

	public function testHashWithSecret(): void
	{
		$secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';

		$this->assertEquals('d08f5a62eab1c1f7a123f947d57f73e7', $this->x->hash('test', null, $secret));
	}

	public function testReset(): void
	{
		$this->assertEquals(true, $this->x->reset());
	}

	public function testUpdateAndDigest(): void
	{
		$this->assertEquals(true, $this->x->update('te'));

		$this->assertEquals(true, $this->x->update('st'));

		$this->assertEquals('2e790d069a07b05183ff80eedd51d542', $this->x->digest());
	}

	public function testResetWithSeed(): void
	{
		$this->assertEquals(true, $this->x->reset(1));

		$this->assertEquals(true, $this->x->update('te'));

		$this->assertEquals(true, $this->x->update('st'));

		$this->assertEquals('2409be6c8b4ea1577c601857728eb3f6', $this->x->digest());

		$this->assertEquals(true, $this->x->reset(10));

		$this->assertEquals(true, $this->x->update('te'));

		$this->assertEquals(true, $this->x->update('st'));

		$this->assertEquals('7a4aef0e7f4f099ff4673c78b49c1c7a', $this->x->digest());

		$this->assertEquals(true, $this->x->reset(100));

		$this->assertEquals(true, $this->x->update('te'));

		$this->assertEquals(true, $this->x->update('st'));

		$this->assertEquals('0668c111a593fea43a81538f866b929a', $this->x->digest());
	}

	public function testResetWithBadSecret(): void
	{
		$this->expectExceptionMessage('XxHash XXH3 secret is too small.');

		$this->x->reset(null, 'not long enough secret');
	}

	public function testResetWithSecret(): void
	{
		$secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';

		$this->assertEquals(true, $this->x->reset(null, $secret));

		$this->assertEquals(true, $this->x->update('te'));

		$this->assertEquals(true, $this->x->update('st'));

		$this->assertEquals('d08f5a62eab1c1f7a123f947d57f73e7', $this->x->digest());
	}

	public function testHashFile(): void
	{
		if($this->x->version() === '0.7.1')
		{
			$this->assertEquals('f755027331532f81e4e3f124d220bf33', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
		}
		else
		{
			$this->assertEquals('c3ead5206079114dce23b624b8f2199f', $this->x->hashFile(__DIR__ . '/../bootstrap.php'));
		}
	}

	public function testHashFileWithSeed(): void
	{
		if($this->x->version() === '0.7.1')
		{
			$this->assertEquals('45e093f9eedf912cfad4e793fbb576ec', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
		}
		else
		{
			$this->assertEquals('3aaea55fc456c7b07e30e17f0a3ee498', $this->x->hashFile(__DIR__ . '/../bootstrap.php', 1));
		}
	}

	public function testHashFileWithSecret(): void
	{
		$secret = '9a5593c8403739049889a795e5fa34627250727170a11f6bf6ff6d8cc53163d3f5d2d69cd0876547797c15358310764836bd91275bdfc4cab884537b386e678a1ab82358';

		if($this->x->version() === '0.7.1')
		{
			$this->assertEquals('38e756e132a79fffcc7323cea1fecc42', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
		}
		else
		{
			$this->assertEquals('2ac5da808cdb875c3c2a9a08b449fa51', $this->x->hashFile(__DIR__ . '/../bootstrap.php', null, $secret));
		}

	}
}
